package com.liu.qqRobot.wm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.thread.ThreadUtil;
import com.liu.common.content.CommonInterface;
import com.liu.common.utils.WmClientUtils;
import com.liu.mybatisPlus.wm.entity.base.*;
import com.liu.mybatisPlus.wm.service.*;
import com.liu.qqRobot.wm.entity.dto.WarframeMarketItemsDTO;
import com.liu.qqRobot.wm.entity.dto.WarframeMarketItemsDetailDTO;
import com.liu.qqRobot.wm.service.SyncItemService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class SyncItemServiceImpl implements SyncItemService {

    @Resource
    private WmItemService wmItemService;

    @Resource
    private WmLichWeaponsService wmLichWeaponsService;

    @Resource
    private WmLichQuirksService wmLichQuirksService;

    @Resource
    private WmLichEphemerasService wmLichEphemerasService;

    @Resource
    private WmSisterWeaponsService wmSisterWeaponsService;

    @Resource
    private WmSisterQuirksService wmSisterQuirksService;

    @Resource
    private WmSisterEphemerasService wmSisterEphemerasService;

    @Resource
    private WmRivenItemService wmRivenItemService;

    @Resource
    private WmRivenAttributesService wmRivenAttributesService;

    @Override
    public void syncItem() {
        List<WmItem> list = wmItemService.list();
        List<String> totalIdList = list.stream().map(WmItem::getId).toList();
        List<String> needModMaxRankItemId = wmItemService.getNeedModMaxRankItemId();
        wmItemService.truncateTable();
        List<WarframeMarketItemsDTO> warframeMarketItemsDTOS = WmClientUtils.getItem();
        List<WmItem> wmItems = BeanUtil.copyToList(warframeMarketItemsDTOS, WmItem.class);
        List<List<WmItem>> wmItemsSplit = ListUtil.split(wmItems, 10);
        wmItemsSplit.parallelStream().forEach(wmItemList -> {
            for (WmItem warframeItem : wmItemList) {
                warframeItem.setThumb(CommonInterface.ITEM_THUMB_URL_HEAD + "/" + warframeItem.getThumb());
                if (!totalIdList.contains(warframeItem.getId()) || needModMaxRankItemId.contains(warframeItem.getId())) {
                    WarframeMarketItemsDetailDTO warframeMarketItemsDetailDTO = null;
                    do {
                        try {
                            warframeMarketItemsDetailDTO = WmClientUtils.getItemDetail(warframeItem.getUrlName());
                            warframeItem.setModMaxRank(warframeMarketItemsDetailDTO.getItemsInSetList().get(0).getModMaxRank());
                        } catch (Exception e) {
                            log.error("获取物品详情失败：{} ex:{}", warframeItem, e.getMessage(), e);
                            ThreadUtil.sleep(1000);
                        }
                    } while (warframeMarketItemsDetailDTO == null);
                }
            }
            wmItemService.saveBatch(wmItemList);
        });
    }

    @Override
    public void syncLich() {
        // 赤毒物品
        List<WarframeMarketItemsDTO> lichWeapons = WmClientUtils.getLichWeapons();
        List<WmLichWeapons> wmLichWeapons = BeanUtil.copyToList(lichWeapons, WmLichWeapons.class);
        wmLichWeapons.forEach(warframeItem -> {
            warframeItem.setThumb(CommonInterface.ITEM_THUMB_URL_HEAD + "/" + warframeItem.getThumb());
        });
        wmLichWeaponsService.truncateTable();
        wmLichWeaponsService.saveBatch(wmLichWeapons, 5000);
        // 赤毒怪癖
        List<WarframeMarketItemsDTO> lichQuirks = WmClientUtils.getLichQuirks();
        List<WmLichQuirks> wmLichQuirks = BeanUtil.copyToList(lichQuirks, WmLichQuirks.class);
        wmLichQuirksService.truncateTable();
        wmLichQuirksService.saveBatch(wmLichQuirks, 5000);
        // 赤毒幻纹
        List<WarframeMarketItemsDTO> lichEphemeras = WmClientUtils.getLichEphemeras();
        List<WmLichEphemeras> wmLichEphemeras = BeanUtil.copyToList(lichEphemeras, WmLichEphemeras.class);
        wmLichEphemeras.forEach(warframeItem -> {
            warframeItem.setThumb(CommonInterface.ITEM_THUMB_URL_HEAD + "/" + warframeItem.getThumb());
        });
        wmLichEphemerasService.truncateTable();
        wmLichEphemerasService.saveBatch(wmLichEphemeras, 5000);
    }

    @Override
    public void syncSister() {
        // 赤毒物品
        List<WarframeMarketItemsDTO> sisterWeapons = WmClientUtils.getSisterWeapons();
        List<WmSisterWeapons> wmSisterWeapons = BeanUtil.copyToList(sisterWeapons, WmSisterWeapons.class);
        wmSisterWeapons.forEach(warframeItem -> {
            warframeItem.setThumb(CommonInterface.ITEM_THUMB_URL_HEAD + "/" + warframeItem.getThumb());
        });
        wmSisterWeaponsService.truncateTable();
        wmSisterWeaponsService.saveBatch(wmSisterWeapons, 5000);
        // 赤毒怪癖
        List<WarframeMarketItemsDTO> lichQuirks = WmClientUtils.getSisterQuirks();
        List<WmSisterQuirks> wmSisterQuirks = BeanUtil.copyToList(lichQuirks, WmSisterQuirks.class);
        wmSisterQuirksService.truncateTable();
        wmSisterQuirksService.saveBatch(wmSisterQuirks, 5000);
        // 赤毒幻纹
        List<WarframeMarketItemsDTO> lichEphemeras = WmClientUtils.getSisterEphemeras();
        List<WmSisterEphemeras> wmSisterEphemeras = BeanUtil.copyToList(lichEphemeras, WmSisterEphemeras.class);
        wmSisterEphemeras.forEach(warframeItem -> {
            warframeItem.setThumb(CommonInterface.ITEM_THUMB_URL_HEAD + "/" + warframeItem.getThumb());
        });
        wmSisterEphemerasService.truncateTable();
        wmSisterEphemerasService.saveBatch(wmSisterEphemeras, 5000);
    }

    @Override
    public void syncRiven() {
        List<WarframeMarketItemsDTO> rivenItems = WmClientUtils.getRivenItems();
        List<WmRivenItem> wmRivenItems = BeanUtil.copyToList(rivenItems, WmRivenItem.class);
        wmRivenItems.forEach(warframeItem -> {
            warframeItem.setThumb(CommonInterface.ITEM_THUMB_URL_HEAD + "/" + warframeItem.getThumb());
            warframeItem.setIcon(CommonInterface.ITEM_THUMB_URL_HEAD + "/" + warframeItem.getIcon());
        });
        wmRivenItemService.truncateTable();
        wmRivenItemService.saveBatch(wmRivenItems, 5000);
        List<WarframeMarketItemsDTO> rivenAttributes = WmClientUtils.getRivenAttributes();
        List<WmRivenAttributes> wmRivenAttributes = new ArrayList<>();
        rivenAttributes.forEach(rivenAttribute -> {
            WmRivenAttributes wmRivenAttribute = BeanUtil.copyProperties(rivenAttribute, WmRivenAttributes.class);
            wmRivenAttribute.setExclusiveTo(CollUtil.join(rivenAttribute.getExclusiveTo(), ","));
            wmRivenAttributes.add(wmRivenAttribute);
        });
        wmRivenAttributesService.truncateTable();
        wmRivenAttributesService.saveBatch(wmRivenAttributes, 5000);
    }

}
